package com.gyp.manager;

import com.alibaba.fastjson.JSON;
import com.gyp.config.properties.IamConfig;
import com.gyp.constant.RedisKeyConstant;
import com.gyp.dto.rpc.TokenRequestDTO;
import com.gyp.exception.BusinessException;
import com.gyp.feign.TokenFeignClient;
import com.gyp.utils.RedisUtils;
import feign.Response;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.Collection;
import java.util.concurrent.TimeUnit;

/**
 * @author xia
 * @Description: 华为云相关
 * @date 2022/12/28
 */
@Component
@Slf4j
public class PersonManager {

    @Autowired
    private TokenFeignClient tokenFeignClient;

    @Autowired
    private IamConfig iamConfig;


    public String getToken() {
        String token =  RedisUtils.getValueOfString(String.format(RedisKeyConstant.IAM_TOKEN,iamConfig.getProjectId()));
        if (StringUtils.isBlank(token)) {
            TokenRequestDTO.TokenPasswordRequest password = TokenRequestDTO.TokenPasswordRequest.builder()
                    .user(TokenRequestDTO.TokenIAMUserRequest.builder()
                            .name(iamConfig.getUser())
                            .password(iamConfig.getPassword())
                            .domain(TokenRequestDTO.TokenDomainRequest.builder()
                                    .name(iamConfig.getName())
                                    .build())
                            .build()).build();

            TokenRequestDTO param = TokenRequestDTO.builder()
                    .auth(TokenRequestDTO.TokenAuthRequest.builder()
                            .scope(TokenRequestDTO.TokenScopeRequest.builder()
                                    .project(TokenRequestDTO.TokenProjectRequest
                                            .builder()
                                            .name(iamConfig.getRegion())
                                            .build())
                                    .build())
                            .identity(TokenRequestDTO.TokenIdentityRequest
                                    .builder()
                                    .methods(Arrays.asList("password"))
                                    .password(password).build())
                            .build()).build();
            log.info("请求参数:{}", JSON.toJSONString(param));
            Response response = tokenFeignClient.getToken(param);
            Collection<String> collection = response.headers().get("x-subject-token");
            if (collection.isEmpty()) {
                throw new BusinessException("10058");
            }
            token = collection.stream().findFirst().get();
            RedisUtils.setValueWithExpire(String.format(RedisKeyConstant.IAM_TOKEN,iamConfig.getProjectId()), token, TimeUnit.HOURS, 10L);
            return token;
        } else {
            return token;
        }
    }

    public void clearToken(){
        RedisUtils.delKey(String.format(RedisKeyConstant.IAM_TOKEN,iamConfig.getProjectId()));
    }
}
